package com.yanqu.road.server.manger.activity.shanshui;

import com.yanqu.road.dao.impl.activity.xingyoushanshui.XingYouShanShuiConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shanshui.XingYouShanShuiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XingYouShanShuiMgr extends TempMgr {

    private static XingYouConfig config;

    @Override
    public boolean init() throws Exception {
        ActivityMgr.getExtendLimitHandlerMap().put(eActivityType.XingYouShanShui.getValue(), new XingYouActivityBuyLimit());
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    /**
     * 重新加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload Shan Shui start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.XingYouShanShui.getValue());
        if (list.isEmpty()) {
            config = null;
            getLogger().info("reload Shan Shui end, no activity in show time.");
            return;
        }
        ActivityInfo activityInfo = list.get(0);

        loadConfigFromDb(activityInfo);
        getLogger().info("reload Shan Shui end, activityId {}.", config.getActivityInfo().getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(XingYouShanShuiModule.class).activityReload();
        }
    }

    /**
     * 数据库加载配置
     */
    private static void loadConfigFromDb(ActivityInfo activityInfo) {
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        XingYouConfig tmpConfig = new XingYouConfig();
        tmpConfig.setActivityInfo(activityInfo);

        XingYouShanShuiConfigDaoImpl configDao = new XingYouShanShuiConfigDaoImpl();
        tmpConfig.setRoleInfoList(configDao.getXingYouShanShuiRoleInfoList(activityInfo.getActivityId()));
        tmpConfig.setEventInfoList(configDao.getXingYouShanShuiEventInfoList(activityInfo.getActivityId()));
        tmpConfig.setGridInfoList(configDao.getXingYouShanShuiGridInfoList(activityInfo.getActivityId()));

        //现金商城礼包
//        Map<Integer, MallInfo> mallInfoMap = NormalActivityMgr.getMallInfoTypeMap(activityInfo.getActivityId(), eShopType.XingYouShanShuiRmbMall.getValue());
//        for(MallInfo item : mallInfoMap.values()) {
//            String[] str = item.getExtendParam().split("\\|");
//            int type = Integer.valueOf(str[0]);
//            int vip = Integer.valueOf(str[1]);
//            Map<Integer, MallInfo> typeMap = tmpConfig.getRmbMallInfoTypeMap().computeIfAbsent(type, k -> new HashMap<>());
//            typeMap.put(vip,item);
//        }

        // 初始化配置
        tmpConfig.initConfig(activityConfigMap);
        config = tmpConfig;

    }

    @Override
    public boolean stop() {
        return true;
    }

    public static XingYouConfig getConfig() {
        return config;
    }

    public static int getActivityId(){
        if(config == null){
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }
}
